/*
 * Copyright (C) 2014 Lucid Wolf
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package com.lucidhex.equation.parser.expr;

import com.lucidhex.equation.parser.Expr;

/**
 *  
 * @author Lucid Wolf
 */
public class UnaryExpr extends Expr {
    int rator;
    Expr rand;

    public UnaryExpr(int rator, Expr rand) { 
	this.rator = rator;
	this.rand = rand;
    }

    @Override
    public double value() {
	double arg = rand.value();
	switch (rator) {
	case ABS:   return Math.abs(arg);
	case ACOS:  return Math.acos(arg);
	case ASIN:  return Math.asin(arg);
	case ATAN:  return Math.atan(arg);
	case CEIL:  return Math.ceil(arg);
	case COS:   return Math.cos(arg);
	case EXP:   return Math.exp(arg);
	case FLOOR: return Math.floor(arg);
	case LOG:   return Math.log(arg);
	case NEG:   return -arg;
	case ROUND: return Math.rint(arg);
	case SIN:   return Math.sin(arg);
	case SQRT:  return Math.sqrt(arg);
	case TAN:   return Math.tan(arg);
	default: throw new RuntimeException("BUG: bad rator");
	}
    }
}
